package com.cnstrong.webview2

import android.content.Context
import android.text.TextUtils
import android.view.View
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import org.json.JSONException
import org.json.JSONObject

open class X5WebViewBuilder(
    val context: Context
) {


    open fun configWebViewInstance(webView: X5WebView) {
        val webSetting: WebSettings = webView.settings
        webSetting.allowFileAccess = true
        webSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webSetting.setSupportZoom(true)
        webSetting.builtInZoomControls = true
        webSetting.useWideViewPort = true
        webSetting.setSupportMultipleWindows(false)
        // webSetting.setLoadWithOverviewMode(true);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true)
        // webSetting.setDatabaseEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.domStorageEnabled = true
        webSetting.javaScriptEnabled = true
        webSetting.setGeolocationEnabled(true)
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE)
        webSetting.setAppCachePath(context.getDir("appcache", 0).path)
        webSetting.databasePath = context.getDir("databases", 0).path
        webSetting.setGeolocationDatabasePath(
            context.getDir("geolocation", 0)
                .path
        )
        webSetting.setPluginState(com.tencent.smtt.sdk.WebSettings.PluginState.ON_DEMAND)
        webSetting.userAgentString = webSetting.userAgentString + " hybrid_1.0.0 "
    }

    open fun registerJsCall(x5WebView: X5WebView) {

    }

    open fun buildJsBridgeDeal(x5WebView: X5WebView): JsBridgeDeal {
        return JsBridgeDealImpl(x5WebView)
    }

    open fun generateX5WebViewInstance(x5WebView: X5WebView?): X5WebView {
        return X5WebView(context)
    }


    fun build(): X5WebView {
        val instance = generateX5WebViewInstance(null)
        configWebViewInstance(instance)
        registerJsCall(instance)
        return instance
    }
}


class JsBridgeDealImpl(private val x5WebView: X5WebView) : JsBridgeDeal {

    override fun getWebViewInstance(): WebView {
        return x5WebView
    }

    override fun registerHtmlDataHandler(name: String, handler: HtmlDataHandler) {
        x5WebView.registerHtmlHandler(name, handler)
    }

    override fun unRegisterHtmlDataHandler(name: String) {
        x5WebView.unRegisterHtmlHandler(name)
    }

    override fun callJs(method: String, data: String?) {
        // 还没有定义javascript:hybrid.callJs($jsonObject)
        if (TextUtils.isEmpty(method)) {
            return
        }
        val jsonObject = JSONObject()
        try {
            jsonObject.put("call", method)
            jsonObject.put("data", data)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        x5WebView.loadUrl("javascript:hybrid.callJs($jsonObject)")
    }

    override fun responseJsCall(callback: String, data: String?) {
        if (TextUtils.isEmpty(callback)) {
            return
        }
        val jsonObject = JSONObject()
        try {
            jsonObject.put("callback", callback)
            jsonObject.put("data", data)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        x5WebView.loadUrl("javascript:hybrid.callback($jsonObject)")
    }

}